package cn.bellychang.queue.bobo.linkedqueue01addfirstandremovelast;

import cn.bellychang.iiterface.liuxin.List;

/**
 * @author: bellychang
 * @date: 2020/2/10
 */
public class LinkedList<E> implements List<E> {

    private Node<E> head;

    private int size;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        Node<E> temp = head.next;
        for (int i = 0; i < size; i++) {
            builder.append(temp.value)
                    .append("->");
            temp = temp.next;
        }
        return builder.length() >= 2 ? builder.toString().substring(0, builder.length() - 2) : builder.toString();
    }

    public LinkedList() {
        Node<E> dummyHead = new Node<>(null, null);
        head = dummyHead;
        size = 0;


    }

    /**
     * {@link:java编程思想:generics}
     * 对于一个static的方法而言，无法访问泛型类的类型参数。
     * 所以，如果static方法需要使用泛型能力，就必须使其成为泛型方法
     *
     * similarly, static class need to declare generics
     * @param <T>
     */
    private static class Node<T> {
        private T value;
        private Node<T> next;

        public Node(T value) {
            this.value = value;
        }

        public Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            return value.toString();
        }
    }

    @Override
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException();
        }
        Node<E> prevNode = head;
        for (int i = 0; i < index; i++) {
            prevNode = prevNode.next;
        }
        prevNode.next = new Node<>(e, prevNode.next);
        size++;
    }

    @Override
    public void addLast(E e) {
        add(size, e);
    }

    public void addFirst(E e) {
        add(0, e);
    }

    @Override
    public E get(int index) {
        checkIndex(index);
        Node<E> prevNode = head;
        for (int i = 0; i < index; i++) {
            prevNode = prevNode.next;
        }
        return prevNode.next.value;
    }

    public E getFirst() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    @Override
    public E remove(int index) {
        checkIndex(index);
        Node<E> prevNode = head;
        for (int i = 0; i < index; i++) {
            prevNode = prevNode.next;
        }
        Node<E> currentNode = prevNode.next;
        E result = currentNode.value;
        prevNode.next = currentNode.next;
        currentNode.next = null;
        size--;
        return result;
    }

    private void checkIndex(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException();
        }
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    @Override
    public int getSize() {
        return this.size;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}
